/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.sms.controller;

import java.io.ByteArrayInputStream;
import java.util.Date;
import java.util.List;

import queenb.net.entitybean.M300;
import queenb.net.entitybean.N100;
import queenb.web.controller.QueenbController;
import queenb.web.sms.store.M300Store;
import queenb.web.util.SessionProvider;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.http.impl.client.DefaultHttpClient;
import queenb.net.entitybean.*;
import queenb.web.crm.store.CustomerStore;
import queenb.web.store.EmployeeStore;
import queenb.web.util.Util;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Scanner;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.http.client.HttpClient;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import queenb.web.util.PackageContants;
import queenb.web.util.SMSUTIL;

/**
 *
 * @author HP
 */
public class M300Controller extends QueenbController {

    private int pm300;
    private Date md301;
    private String mv303;//trang thai
    private String mv302;//noi dung tin nhan
    private String mv304;
    private String mv305;
    private String mv306;
    private String mv307;
    private String mv308;
    private String mv309;
    private String mv310;
    private int pn100;
    private M300Store m300Store;
    private int beginContructor;
    private String keySearch;
    private int typesearch;
    //paging
    private int Offset = 0;
    private int Limit = 30;
    private int totalrow; // tong so dong
    private int totalpage; // tong so trang
    private int current; // traung hien tai
    static private int numrow = 30; // so dong tren 1 trang
    private int start; // bat dau lay tu record
    private int end; // lay tu record so
    private int startpage = 1; // page bat dau
    private int endpage = 1;   // page ket thuc
    private String pagename = "";
    private int totalcurent;
    private int mn314;
    private int typesearchcurent;
    private List<M300> listM300US;
    private List<M300> listM300AL;
    private List<MailItem> listMail = new ArrayList<MailItem>();
    private String s = "";
    private int ni104 = 1;//khach hang tiem nang
    private int fk400;
    private int dayss;
    private int stephide;// phan biet trang de reload
    private long timezone;
    private int fn850;// phong ban
    private int countRow;// bao nhieu nguoi dc gui

    public M300Controller() {
        getKapiMenu().setAdmin(false);
        getKapiMenu().setMainmenu1("m192");
        m300Store = new M300Store();
        Object objAction = getRequest().getParameter("action");
        Object objPN100 = getRequest().getParameter("pn100");
        if (objPN100 != null) {
            this.pn100 = Integer.parseInt(objPN100.toString());
        }
        Object type = getRequest().getParameter("type");
        if (type != null) {
            ni104 = Integer.parseInt(type.toString());
            SessionProvider.setSessionMapValue("customertype", ni104);
        }
        Object objDayss = getRequest().getParameter("dayss");
        if (objDayss != null) {
            dayss = Integer.parseInt(objDayss.toString());
        }
        Object objPK400 = getRequest().getParameter("pk400");
        if (objPK400 != null) {
            fk400 = Integer.parseInt(objPK400.toString());
        }
        if (objAction != null) {
            if (objAction.equals("sendSMS")) {
                Object objMV304 = getRequest().getParameter("phone");
                if (objMV304 != null) {
                    this.mv304 = objMV304.toString();//so dien thoai
                }
                Object objMV302 = getRequest().getParameter("mv302");
                if (objMV302 != null) {
                    this.mv302 = objMV302.toString();//noi dung ti nnhan
                }
                Object objMV305 = getRequest().getParameter("mv305");
                if (objMV305 != null) {
                    this.mv305 = objMV305.toString();//muc dich
                }
                Object objMV306 = getRequest().getParameter("mv306");
                System.out.print("--mv306---------" + objMV306);
                if (objMV306 != null) {
                    this.mv306 = objMV306.toString();//Tour
                }
                System.out.println("-------pn100-----------" + pn100 + "----mv036---" + mv306 + "----MV304--" + mv304);
                if (mv304 != null) {
                    if (mv304.length() > 9) {
                        updateM300();
                    }

                }

            } else if (objAction.equals("groupMN134")) {
                Object objMN134 = getRequest().getParameter("mn314");
                if (objMN134 != null) {
                    this.mn314 = Integer.parseInt(objMN134.toString());
                }
                System.out.println("---------mn314---------" + mn314);
                if (mn314 > 0) {//kiem tra id > 0 moi get SMS
                    getListM300US();
                }

            } else if (objAction.equals("sendMoreSMS")) {
                System.out.println("SMS - SAVE SMS");
                Object objMV302 = getRequest().getParameter("mv302");
                if (objMV302 != null) {
                    this.mv302 = objMV302.toString();//noi dung ti nnhan
                }
                Object objMV305 = getRequest().getParameter("mv305");
                if (objMV305 != null) {
                    this.mv305 = objMV305.toString();//muc dich
                }
                List<MailItem> list = new ArrayList<MailItem>();
                if (SessionProvider.getSessionMapValue("SMS") != null) {
                    list = (List<MailItem>) SessionProvider.getSessionMapValue("SMS");
                }
                if (list != null) {
                    System.out.println("---------list size ----------" + list.size());
                    Iterator iterator = list.iterator();
                    M300Store m300Store = new M300Store();
                    this.mn314 = m300Store.selectIDM300("M300", "MN314", getLogin().getModule(), 1);
                    while (iterator.hasNext()) {
                        MailItem n100 = (MailItem) iterator.next();
//                        System.out.println("---------insert ----------" + n100.getVv051() + "---" + n100.getVv052());
                        try {
                            java.sql.Timestamp date = new java.sql.Timestamp(new Date().getTime());
                            System.out.println("---dien thoai---" + n100.getPhone());
                            if (n100.getPhone() != null && !n100.getPhone().equals("")) {
                                m300Store.updateM300(0, getLogin().getModule(), getLogin().getCompanyNo(), n100.getIditem(), date, this.getMv302(), this.getMv303(), n100.getPhone(), this.getMv305(), n100.getCodeTour(), this.getMn314(), getLogin().getUserName());
                            }
                        } catch (Exception ex) {
                            System.out.println("D:- Error from send SMS: " + ex.getMessage());
                        }
                    }
                }
            }
        }

        //paging
        int a;
//        System.out.println("D:-- listEmployee is not null ");
        Object ppage = getRequest().getParameter("p");
        if (ppage != null) {
            try {
                a = Integer.parseInt(ppage.toString());
            } catch (Exception ex) {
                a = 1;
            }
            if (a == 1) {
                setCurrent(1);
                setOffset(0);
                System.out.println("D:-- setOffset: " + 0);
            } else {
                setCurrent(a);
                setOffset((a - 1) * Limit);
                System.out.println("D:-- setOffset: " + ((a - 1) * Limit));
            }
            System.out.println("D:-- page: " + a);

        } else {
            SessionProvider.setSessionMapValue("typeSearchSMS", null);
            SessionProvider.setSessionMapValue("keySearchSMS", null);
            setCurrent(1);
            setOffset(0);
            System.out.println("D:-- page: 1");

        }

        //Duy send SMS
        Object objpage = getRequest().getParameter("page");
//        listMail.clear();
        List<N100> listKH = new ArrayList<N100>();
        List<K100> listKHCT = new ArrayList<K100>();
        List<B630> listCSKH = new ArrayList<B630>();
        if (objpage != null) {
            s = objpage.toString();
            if (s.equals("customer4tour")) {
                Object listselect = SessionProvider.getSessionMapValue("listSelected");
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                        countRow = listKH.size();

                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
                //listKH = V030controller.listSelected;

            } else if (s.equals("employeeUser")) {
                System.out.println("----------VAO 111---------------");
                Object listselect = SessionProvider.getSessionMapValue("listSelectedEx");
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                        countRow = listKH.size();
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
                //listKH = CustomerExpand.listSelected;
            } else if (s.equals("customerexpand")) {
                Object listselect = SessionProvider.getSessionMapValue("listSelectedEx");
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                        countRow = listKH.size();
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
                //listKH = CustomerExpand.listSelected;
            } //workChargeCM
            else if (s.equals("workChargeCM")) {
                Object listselect = SessionProvider.getSessionMapValue("listSelectedB630CM");
                if (listselect != null) {
                    try {
                        listCSKH = (List<B630>) listselect;
                    } catch (Exception e) {
                        listCSKH = new ArrayList<B630>();
                    }
                } else {
                    listCSKH = new ArrayList<B630>();
                }
            } // TUng send SMS for listEmployee and listPersonal
            else if (s.equals("listEmployee")) {
                Object listselect = SessionProvider.getSessionMapValue("listPersonalDSVN");
                Object objectStep = getRequest().getParameter("step");

                if (objectStep != null) {
                    stephide = Util.parseInt(objectStep);
                }
                Object objectFk100 = getRequest().getParameter("fk100reload");
                if (objectFk100 != null) {
                    fk400 = Util.parseInt(objectFk100);
                }

                System.out.println("---------- LIDST DTDTDTDTD-TTTTTTTTTTTTTTTT-----" + listselect);
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
            } else if (s.equals("listPersonal")) {
                Object objectStep = getRequest().getParameter("step");

                if (objectStep != null) {
                    stephide = Util.parseInt(objectStep);
                }
                System.out.println("-----------------------------------)))))))))))--------" + stephide);
                Object objectFk100 = getRequest().getParameter("fk100reload");
                if (objectFk100 != null) {
                    fk400 = Util.parseInt(objectFk100);
                }
                Object listselect = SessionProvider.getSessionMapValue("listPersonalDSDD");
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
            } // the endn Tung
            else if (s.equals("workChargeCOM")) {
                Object listselect = SessionProvider.getSessionMapValue("listSelectedB630COM");
                if (listselect != null) {
                    try {
                        listCSKH = (List<B630>) listselect;
                    } catch (Exception e) {
                        listCSKH = new ArrayList<B630>();
                    }
                } else {
                    listCSKH = new ArrayList<B630>();
                }
            } else if (s.equals("employee")) {
                Object listselect = SessionProvider.getSessionMapValue("ListSMSEmployee");
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
            } else if (s.equals("customer")) {
                Object listselect = SessionProvider.getSessionMapValue("ListSMSCustomer");
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
            } else if (s.equals("cuscompany")) {
                Object listselect = SessionProvider.getSessionMapValue("ListSMSCustommerCompany");
                if (listselect != null) {
                    try {
                        listKHCT = (List<K100>) listselect;
                    } catch (Exception e) {
                        listKHCT = new ArrayList<K100>();
                    }
                } else {
                    listKHCT = new ArrayList<K100>();
                }
            } else if (s.equals("employeeN")) {
                Object objectStep1 = getRequest().getParameter("step");

                if (objectStep1 != null) {
                    stephide = Util.parseInt(objectStep1);
                }
                Object listselect = getListAllEmployeeSendSMS_Email();
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
            } else if (s.equals("companySMS")) {
                Object objectStep1 = getRequest().getParameter("step");

                if (objectStep1 != null) {
                    stephide = Util.parseInt(objectStep1);
                }
                Object listselect = getListAllCompanySendSMS_Email();
                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                        countRow = listKH.size();
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
            } else if (s.equals("customerSMSforUserLogin")) {
                Object objectStep1 = getRequest().getParameter("step");

                if (objectStep1 != null) {
                    stephide = Util.parseInt(objectStep1);
                }
                Object listselect = null;
                if (SessionProvider.getSessionMapValue("listSelectedEx") != null) {
                    listselect = SessionProvider.getSessionMapValue("listSelectedEx");
                } else {
                    listselect = getListAllCustomerforUserlogin();
                }

                if (listselect != null) {
                    try {
                        listKH = (List<N100>) listselect;
                        countRow = listKH.size();
                    } catch (Exception e) {
                        listKH = new ArrayList<N100>();
                    }
                } else {
                    listKH = new ArrayList<N100>();
                }
            }



        }

        if (s.equals("workChargeCM") || s.equals("workChargeCOM")) {
            for (int i = 0; i < listCSKH.size(); i++) {
                B630 cus = listCSKH.get(i);
                if (cus.getNv108() != null && cus.getNv108() != "") {
                    MailItem mail = new MailItem(cus.getFq100(), cus.getHoten(), "", "," + cus.getNv108(), "Kính gửi quý khách: ", "");
                    listMail.add(mail);
//                    listInfo += listCSKH.get(i).getNv105() + ";  ";
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("customerSMSforUserLogin")) {
            System.out.println("-------DANH SAHCH KHAC HANG TUNG CA NHAN------" + listKH.size());
            for (int i = 0; i < listKH.size(); i++) {
                N100 cus = listKH.get(i);
                if (cus.getNv958() != null && cus.getNv958() != "") {
                    MailItem mail = new MailItem(cus.pn100, cus.getHoten(), "", "," + cus.getNv958(), cus.getPronouns(), "");
                    listMail.add(mail);
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("companySMS")) {
            System.out.println("---------- LIDST DTDTDTDTD------" + listKH.size());
            for (int i = 0; i < listKH.size(); i++) {
                N100 cus = listKH.get(i);
                if (cus.getNv958() != null && cus.getNv958() != "") {
                    MailItem mail = new MailItem(cus.pn100, cus.getHoten(), "", "," + cus.getNv958(), cus.getPronouns(), "");
                    listMail.add(mail);
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("listEmployee") || s.equals("listPersonal")) {
            for (int i = 0; i < listKH.size(); i++) {
                N100 cus = listKH.get(i);
                if (cus.getNv121() != null && cus.getNv121() != "") {
                    MailItem mail = new MailItem(cus.pn100, cus.getHoten(), "", "," + cus.getNv121(), cus.getPronouns(), "");
                    //  mail.setCodeTour(cus.getVv051());
                    listMail.add(mail);
//                    listInfo += listKH.get(i).getNv105() + ";  ";
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("customer4tour") || s.equals("customerexpand")) {
            for (int i = 0; i < listKH.size(); i++) {
                N100 cus = listKH.get(i);
                if (cus.getNv958() != null && cus.getNv958() != "") {
                    MailItem mail = new MailItem(cus.pn100, cus.getHoten(), "", cus.getNv958(), cus.getPronouns(), "");
                    mail.setCodeTour(cus.getVv051());
                    listMail.add(mail);
//                    listInfo += listKH.get(i).getNv105() + ";  ";
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("employee")) {
            for (int i = 0; i < listKH.size(); i++) {
                N100 cus = listKH.get(i);
                if (cus.getNv121() != null && cus.getNv121() != "") {
                    MailItem mail = new MailItem(cus.pn100, cus.getHoten(), "", "," + cus.getNv121(), cus.getPronouns(), "");
                    listMail.add(mail);
//                    listInfo += listKH.get(i).getNv105() + ";  ";
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("employeeUser")) {
            System.out.println("-----------VAOD AY LÍT ------");
            for (int i = 0; i < listKH.size(); i++) {
                N100 cus = listKH.get(i);
                if (cus.getNv121() != null && cus.getNv121() != "") {
                    MailItem mail = new MailItem(cus.pn100, cus.getHoten(), "", "," + cus.getNv121(), cus.getPronouns(), "");
                    listMail.add(mail);

                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("customer")) {
            for (int i = 0; i < listKH.size(); i++) {
                N100 cus = listKH.get(i);
                if (cus.getNv958() != null && cus.getNv958() != "") {
                    MailItem mail = new MailItem(cus.pn100, cus.getHoten(), "", "," + cus.getNv958(), cus.getPronouns(), "");
                    listMail.add(mail);
//                    listInfo += listKH.get(i).getNv105() + ";  ";
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("cuscompany")) {
            for (int i = 0; i < listKHCT.size(); i++) {
                K100 cus = listKHCT.get(i);
                if (cus.getKv961() != null && cus.getKv961() != "") {
                    MailItem mail = new MailItem(cus.pk100, cus.getKv103(), "", "," + cus.getKv961(), "Kính gửi quý khách", "");
                    listMail.add(mail);
//                    listInfo += listKH.get(i).getNv105() + ";  ";
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        } else if (s.equals("employeeN")) {
            for (int i = 0; i < listKH.size(); i++) {
                N100 cus = listKH.get(i);
                if (cus.getNv121() != null && cus.getNv121() != "") {
                    MailItem mail = new MailItem(cus.pn100, cus.getHoten(), "", "," + cus.getNv121(), cus.getPronouns(), "");
                    listMail.add(mail);
//                    listInfo += listKH.get(i).getNv105() + ";  ";
                }

            }
            SessionProvider.setSessionMapValue("SMS", listMail);
        }

        System.out.println("DANH DACH SDT");
        for (MailItem item : getListMail()) {
            System.out.println("," + item.getPhone());
        }

    }

    // List All Eployee
    public List<N100> getListAllEmployeeSendSMS_Email() {
        try {

            EmployeeStore employeeStore = new EmployeeStore();
            if (SessionProvider.getSessionMapValue("IDSENDSMSMAIL") != null) {
                fn850 = Util.parseInt(SessionProvider.getSessionMapValue("IDSENDSMSMAIL"));
            } else {
                fn850 = Util.parseInt(SessionProvider.getSessionMapValue("groupID"));
            }

            List<N100> listAll = employeeStore.getAllListTabN100All(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, fn850, 0, null, 0, 1000, 0, 0, getLogin().getUserName());
            SessionProvider.setSessionMapValue("NUMBERPER", listAll.size());
            return listAll;
        } catch (Exception e) {
            return null;
        }
    }

    // List All cútomer
    public List<N100> getListAllCompanySendSMS_Email() {
        try {

            EmployeeStore employeeStore = new EmployeeStore();

            List<N100> listAll = employeeStore.ncurpks_listoftabn100lcon(getLogin().getModule(), getLogin().getCompanyNo(), 2, getLogin().getUserName());
            System.out.println("-------------LIST-----------------" + listAll.size());
            return listAll;
        } catch (Exception e) {
            return null;
        }
    }
    // danh sach khach hang cho tung ca nhan

    public List<N100> getListAllCustomerforUserlogin() {
        try {
            List<N100> listAllCustomer = new ArrayList<N100>();
            CustomerStore customerStore = new CustomerStore();
            if (SessionProvider.getSessionMapValue("customertype") != null) {
                listAllCustomer = customerStore.getListCustomerALL(getLogin().getModule(), getLogin().getCompanyNo(), 0, Util.parseInt(SessionProvider.getSessionMapValue("IDSMSOFCUSTOMER")), null, 0, 10000, 0, 0, getLogin().getUserName());

            } else {
                listAllCustomer = customerStore.getListCustomerALL(getLogin().getModule(), getLogin().getCompanyNo(), 0, 1, null, 0, 10000, 0, 0, getLogin().getUserName());
            }


            SessionProvider.setSessionMapValue("NUMBERPER", listAllCustomer.size());
            return listAllCustomer;
        } catch (Exception e) {
            return null;
        }
    }

    public void sendSMS(String smsMessage, String smsPhone) {
        //
        String smsClientNo = getResourceString("sms.clientno");
        String smsClientPass = getResourceString("sms.clientpass");
        String smsGuiID = getResourceString("sms.smsGUID");
        String smsServiceType = getResourceString("sms.servicetype");
        String url = getResourceString("sms.urlservice");
        HttpClient client = new DefaultHttpClient();

//        HttpGet request = new HttpGet(url + "clientNo=" + smsClientNo + "&clientPass=" + smsClientPass + "&phoneNumber=" + smsPhone + "&smsMessage=" + smsMessage + "&smsGUID=" + smsGuiID + "&serviceType=" + smsServiceType);
//        HttpResponse response = client.execute(request);
//
//        // Get the response
//        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
//
//        String line = "";
//        while ((line = rd.readLine()) != null) {
//            System.out.println(line);

    }

    public void updateM300() {
        try {
            java.sql.Timestamp date = new java.sql.Timestamp(new Date().getTime());
            this.mn314 = m300Store.selectIDM300("M300", "MN314", getLogin().getModule(), 1);
            System.out.println("---mntab id-------------" + this.mn314);
            m300Store.updateM300(0, getLogin().getModule(), getLogin().getCompanyNo(), this.getPn100(), date, this.getMv302(), this.getMv303(), this.getMv304(), this.getMv305(), this.getMv306(), this.getMn314(), getLogin().getUserName());
        } catch (Exception ex) {
        }
    }

    public List<M300> getListM300() {
        System.out.println("D:- getListM300 is called !");
        List<M300> listM300 = null;
        try {
            java.sql.Timestamp date = new java.sql.Timestamp(new Date().getTime());
            if (SessionProvider.getSessionMapValue("submitSearch") != null && SessionProvider.getSessionMapValue("submitSearch").toString().equals("true")) {
                setTypesearch(3);
                setKeySearch(getLogin().getCharacter());

            }

            if (getKeySearch() != null && getKeySearch().contains("Nhập từ khoá")) {
                setKeySearch(getLogin().getCharacter());
            }

            Object o = SessionProvider.getSessionMapValue("typeSearchSMS");
            if (o != null) {
                int type;
                try {
                    type = Integer.parseInt(o.toString());
                } catch (Exception e) {
                    type = 0;
                }
                setTypesearch(type);

            }

            Object o2 = SessionProvider.getSessionMapValue("keySearchSMS");
            if (o2 != null) {
                setKeySearch(o2.toString());



            }

            if (listM300 == null) {
                if (getTypesearch() == 1) {
                    System.out.println("D:- getTypesearch() == 1 !  ---  Tim kiem theo SDT --- " + "Keysearch: " + getKeySearch());
                    System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getKeySearch() + ", 0, 1000, 0, 0," + getLogin().getUserName());
                    listM300 = m300Store.getListM300MO(getLogin().getModule(), getLogin().getCompanyNo(), date, this.getMv303(), getKeySearch(), 0, 1000, 0, 0, getLogin().getUserName());
                    System.out.println("--listsize :  " + listM300.size());
                } else if (getTypesearch() == 2) {
                    System.out.println("D:- getTypesearch() == 2 !  ---  Tim kiem theo TOUR --- " + "Keysearch: " + getKeySearch());
                    System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getKeySearch() + ", 0, 1000, 0, 0," + getLogin().getUserName());
                    listM300 = m300Store.getListM300TU(getLogin().getModule(), getLogin().getCompanyNo(), date, this.getMv303(), getKeySearch(), 0, 1000, 0, 0, getLogin().getUserName());
                    System.out.println("--listsize :  " + listM300.size());
                } else if (getTypesearch() == 0) {
                    System.out.println("D:- getTypesearch() == 0 !  ---  Tim kiem tat ca (chua co ham) --- " + "Keysearch: " + getKeySearch());
                    System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getLogin().getUserName());
                    listM300 = m300Store.getListM300(getLogin().getModule(), getLogin().getCompanyNo(), this.getPn100(), date, this.getMv303(), getLogin().getUserName());
                    System.out.println("--listsize :  " + listM300.size());
                } else {
                    System.out.println("D:- getTypesearch() == null!  ---  lay tat ca --- " + "Keysearch: " + getKeySearch());
                    System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getLogin().getUserName());
                    listM300 = m300Store.getListM300(getLogin().getModule(), getLogin().getCompanyNo(), this.getPn100(), date, this.getMv303(), getLogin().getUserName());
                    System.out.println("--listsize :  " + listM300.size());
                }
            }
            return listM300;
        } catch (Exception ex) {
            System.out.println("D:--  Error from getListM300: " + ex.getMessage());
            return null;
        }

    }

    public List<M300> getListM300US() {

        try {
            System.out.println("------------list mvn--------" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getMn314() + "," + getLogin().getUserName());
            return m300Store.getListM300US(getLogin().getModule(), getLogin().getCompanyNo(), this.getMn314(), getLogin().getUserName());

        } catch (Exception ex) {
        }
        return null;
    }

    public List<M300> getListM300AL() {
        System.out.println("D:- getListM300 is called !");
        List<M300> listM300 = null;
        try {
            java.sql.Timestamp date = new java.sql.Timestamp(new Date().getTime());
            if (SessionProvider.getSessionMapValue("submitSearch") != null && SessionProvider.getSessionMapValue("submitSearch").toString().equals("true")) {
                setTypesearch(3);
                setKeySearch(getLogin().getCharacter());

            }

            if (getKeySearch() != null && getKeySearch().contains("Nhập từ khoá")) {
                setKeySearch(getLogin().getCharacter());
            }

            Object o = SessionProvider.getSessionMapValue("typeSearchSMS");
            if (o != null) {
                int type;
                try {
                    type = Integer.parseInt(o.toString());
                } catch (Exception e) {
                    type = 0;
                }
                setTypesearch(type);

            }

            Object o2 = SessionProvider.getSessionMapValue("keySearchSMS");
            if (o2 != null) {
                setKeySearch(o2.toString());



            }

            if (getTypesearch() == 1) {
                System.out.println("D:- getTypesearch() == 1 !  ---  Tim kiem theo SDT --- " + "Keysearch: " + getKeySearch());
                System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getKeySearch() + ", 0, 1000, 0, 0," + getLogin().getUserName());
                listM300 = m300Store.getListM300MO(getLogin().getModule(), getLogin().getCompanyNo(), date, this.getMv303(), getKeySearch(), 0, 1000, 0, 0, getLogin().getUserName());
                System.out.println("--listsize :  " + listM300.size());
            } else if (getTypesearch() == 2) {
                System.out.println("D:- getTypesearch() == 2 !  ---  Tim kiem theo TOUR --- " + "Keysearch: " + getKeySearch());
                System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getKeySearch() + ", 0, 1000, 0, 0," + getLogin().getUserName());
                listM300 = m300Store.getListM300NA(getLogin().getModule(), getLogin().getCompanyNo(), date, this.getMv303(), getKeySearch(), 0, 1000, 0, 0, getLogin().getUserName());
                System.out.println("--listsize :  " + listM300.size());
            } else if (getTypesearch() == 3) {
                System.out.println("D:- getTypesearch() == 3 !  ---  Tim kiem theo ten --- " + "Keysearch: " + getKeySearch());
                System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getKeySearch() + ", 0, 1000, 0, 0," + getLogin().getUserName());
                listM300 = m300Store.getListM300TU(getLogin().getModule(), getLogin().getCompanyNo(), date, this.getMv303(), getKeySearch(), 0, 1000, 0, 0, getLogin().getUserName());
                System.out.println("--listsize :  " + listM300.size());
            } else if (getTypesearch() == 0) {
                System.out.println("D:- getTypesearch() == 0 !  ---  Tim kiem tat ca (chua co ham) --- " + "Keysearch: " + getKeySearch());
                System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getLogin().getUserName());
                listM300 = m300Store.getListM300AL(getLogin().getModule(), getLogin().getCompanyNo(), this.getPn100(), date, this.getMv303(), getLogin().getUserName());
                System.out.println("--listsize :  " + listM300.size());
            } else {
                System.out.println("D:- getTypesearch() == null!  ---  lay tat ca --- " + "Keysearch: " + getKeySearch());
                System.out.println("--list----" + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.getPn100() + "," + date + "," + this.getMv303() + "," + getLogin().getUserName());
                listM300 = m300Store.getListM300AL(getLogin().getModule(), getLogin().getCompanyNo(), this.getPn100(), date, this.getMv303(), getLogin().getUserName());
                System.out.println("--listsize :  " + listM300.size());
            }

            return listM300;
        } catch (Exception ex) {
            System.out.println("D:--  Error from getListM300: " + ex.getMessage());
            return null;
        }
//        try {
//            java.sql.Timestamp date = new java.sql.Timestamp(new Date().getTime());
//            return m300Store.getListM300AL(getLogin().getModule(), getLogin().getCompanyNo(), this.getPn100(), date, this.getMv303(), getLogin().getUserName());
//
//        } catch (Exception ex) {
//        }
//        return null;
//    }
    }

    public void reload() {
        System.out.println("---change+++++++++++++" + getKeySearch() + typesearch);
        SessionProvider.setSessionMapValue("submitSearch", "false");
        System.out.println(" ----------D:   Typesearch: " + typesearch);
        System.out.println(" ----------D:   Typesearch: " + keySearch);
        SessionProvider.setSessionMapValue("typeSearchSMS", typesearch);
        SessionProvider.setSessionMapValue("keySearchSMS", keySearch);
    }

    /**
     * @return the pm300
     */
    public int getPm300() {
        return pm300;
    }

    /**
     * @param pm300 the pm300 to set
     */
    public void setPm300(int pm300) {
        this.pm300 = pm300;
    }

    /**
     * @return the md301
     */
    public Date getMd301() {
        return md301;
    }

    /**
     * @param md301 the md301 to set
     */
    public void setMd301(Date md301) {
        this.md301 = md301;
    }

    /**
     * @return the mv303
     */
    public String getMv303() {
        return mv303;
    }

    /**
     * @param mv303 the mv303 to set
     */
    public void setMv303(String mv303) {
        this.mv303 = mv303;
    }

    /**
     * @return the mv304
     */
    public String getMv304() {
        return mv304;
    }

    /**
     * @param mv304 the mv304 to set
     */
    public void setMv304(String mv304) {
        this.mv304 = mv304;
    }

    /**
     * @return the mv305
     */
    public String getMv305() {
        return mv305;
    }

    /**
     * @param mv305 the mv305 to set
     */
    public void setMv305(String mv305) {
        this.mv305 = mv305;
    }

    /**
     * @return the mv306
     */
    public String getMv306() {
        return mv306;
    }

    /**
     * @param mv306 the mv306 to set
     */
    public void setMv306(String mv306) {
        this.mv306 = mv306;
    }

    /**
     * @return the mv307
     */
    public String getMv307() {
        return mv307;
    }

    /**
     * @param mv307 the mv307 to set
     */
    public void setMv307(String mv307) {
        this.mv307 = mv307;
    }

    /**
     * @return the mv308
     */
    public String getMv308() {
        return mv308;
    }

    /**
     * @param mv308 the mv308 to set
     */
    public void setMv308(String mv308) {
        this.mv308 = mv308;
    }

    /**
     * @return the mv309
     */
    public String getMv309() {
        return mv309;
    }

    /**
     * @param mv309 the mv309 to set
     */
    public void setMv309(String mv309) {
        this.mv309 = mv309;
    }

    /**
     * @return the mv310
     */
    public String getMv310() {
        return mv310;
    }

    /**
     * @param mv310 the mv310 to set
     */
    public void setMv310(String mv310) {
        this.mv310 = mv310;
    }

    /**
     * @return the mv302
     */
    public String getMv302() {
        return mv302;
    }

    /**
     * @param mv302 the mv302 to set
     */
    public void setMv302(String mv302) {
        this.mv302 = mv302;
    }

    /**
     * @return the beginContructor
     */
    public int getBeginContructor() {
        return beginContructor;
    }

    /**
     * @param beginContructor the beginContructor to set
     */
    public void setBeginContructor(int beginContructor) {
        this.beginContructor = beginContructor;
    }

    /**
     * @return the pn100
     */
    public int getPn100() {
        return pn100;
    }

    /**
     * @param pn100 the pn100 to set
     */
    public void setPn100(int pn100) {
        this.pn100 = pn100;
    }

    /**
     * @return the keySearch
     */
    public String getKeySearch() {
        return keySearch;
    }

    /**
     * @param keySearch the keySearch to set
     */
    public void setKeySearch(String keySearch) {
        this.keySearch = keySearch;
    }

    /**
     * @return the typesearch
     */
    public int getTypesearch() {
        return typesearch;
    }

    /**
     * @param typesearch the typesearch to set
     */
    public void setTypesearch(int typesearch) {
        this.typesearch = typesearch;
    }

    /**
     * @return the Offset
     */
    public int getOffset() {
        return Offset;
    }

    /**
     * @param Offset the Offset to set
     */
    public void setOffset(int Offset) {
        this.Offset = Offset;
    }

    /**
     * @return the Limit
     */
    public int getLimit() {
        return Limit;
    }

    /**
     * @param Limit the Limit to set
     */
    public void setLimit(int Limit) {
        this.Limit = Limit;
    }

    // property for paging
    /**
     * @return the current
     */
    public int getCurrent() {
        if (current > totalpage) {
            current = totalpage;
        } else if (current < 1) {
            current = 1;
        }
        return current;
    }

    /**
     * @param current the current to set
     */
    public void setCurrent(int current) {

        start = (current - 1) * getNumrow();
        end = current * numrow;
        this.current = current;
    }

    /**
     * @return the numrow
     */
    public int getNumrow() {
        if (numrow == 0) {
            numrow = 30;
        }
        return numrow;
    }

    /**
     * @param numrow the numrow to set
     */
    public void setNumrow(int numrow) {
        this.numrow = numrow;
    }

    /**
     * @return the start
     */
    public int getStart() {
        if (start == 0) {
            start = 1;
        }
        return start;
    }

    /**
     * @param start the start to set
     */
    public void setStart(int start) {
        this.start = start;
    }

    /**
     * @return the end
     */
    public int getEnd() {
        if (end == 0) {
            end = 20;
        }
        return end;
    }

    /**
     * @param end the end to set
     */
    public void setEnd(int end) {
        this.end = end;
    }

    /**
     * @return the totalrow
     */
    public int getTotalrow() {
        return totalrow;
    }

    /**
     * @param totalrow the totalrow to set
     */
    public void setTotalrow(int totalrow) {
//        if (totalrow == 0) {
//            totalrow = 100;
//        }
        totalpage = totalrow / numrow;

        if (totalrow % numrow > 0) {
            totalpage = totalpage + 1;
        }


        if (getTotalpage() <= 10) {
            setStartpage(1);
            setEndpage(getTotalpage());

        } else {
            setStartpage(current - 5);
            setEndpage(current + 5);


        }
        this.totalrow = totalrow;
    }

    /**
     * @return the startpage
     */
    public int getStartpage() {
        if (startpage < 1) {
            startpage = 1;
        }
        return startpage;
    }

    /**
     * @param startpage the startpage to set
     */
    public void setStartpage(int startpage) {
        this.startpage = startpage;
    }

    /**
     * @return the endpage
     */
    public int getEndpage() {
        if (endpage > totalpage) {
            endpage = totalpage;
        }
        return endpage;
    }

    /**
     * @param endpage the endpage to set
     */
    public void setEndpage(int endpage) {

        this.endpage = endpage;
    }

    /**
     * @return the totalpage
     */
    public int getTotalpage() {
//        if (totalpage == 0) {
//            totalpage = 10;
//        }
        return totalpage;
    }

    /**
     * @param totalpage the totalpage to set
     */
    public void setTotalpage(int totalpage) {
        this.totalpage = totalpage;
    }

    /**
     * @return the pagename
     */
    public String getPagename() {
        return pagename;
    }

    /**
     * @param pagename the pagename to set
     */
    public void setPagename(String pagename) {
        this.pagename = pagename;
    }

    /**
     * @return the totalcurent
     */
    public int getTotalcurent() {
        return totalcurent;
    }

    /**
     * @param totalcurent the totalcurent to set
     */
    public void setTotalcurent(int totalcurent) {
        this.totalcurent = totalcurent;
    }

    /**
     * @return the typesearchcurent
     */
    public int getTypesearchcurent() {
        return typesearchcurent;
    }

    /**
     * @param typesearchcurent the typesearchcurent to set
     */
    public void setTypesearchcurent(int typesearchcurent) {
        this.typesearchcurent = typesearchcurent;
    }

    /**
     * @return the mn314
     */
    public int getMn314() {
        return mn314;
    }

    /**
     * @param mn314 the mn314 to set
     */
    public void setMn314(int mn314) {
        this.mn314 = mn314;
    }

    /**
     * @return the listMail
     */
    public List<MailItem> getListMail() {
        return listMail;
    }

    /**
     * @param listMail the listMail to set
     */
    public void setListMail(List<MailItem> listMail) {
        this.listMail = listMail;
    }

    /**
     * @return the s
     */
    public String getS() {
        return s;
    }

    /**
     * @param s the s to set
     */
    public void setS(String s) {
        this.s = s;
    }

    /**
     * @return the ni104
     */
    public int getNi104() {
        return ni104;
    }

    /**
     * @param ni104 the ni104 to set
     */
    public void setNi104(int ni104) {
        this.ni104 = ni104;
    }

    /**
     * @return the fk400
     */
    public int getFk400() {
        return fk400;
    }

    /**
     * @param fk400 the fk400 to set
     */
    public void setFk400(int fk400) {
        this.fk400 = fk400;
    }

    /**
     * @return the dayss
     */
    public int getDayss() {
        return dayss;
    }

    /**
     * @param dayss the dayss to set
     */
    public void setDayss(int dayss) {
        this.dayss = dayss;
    }

    /**
     * @return the stephide
     */
    public int getStephide() {
        return stephide;
    }

    /**
     * @param stephide the stephide to set
     */
    public void setStephide(int stephide) {
        this.stephide = stephide;
    }

    /**
     * @return the timezone
     */
    public long getTimezone() {
        timezone = new Date().getTime();
        return timezone;
    }

    /**
     * @param timezone the timezone to set
     */
    public void setTimezone(long timezone) {
        this.timezone = timezone;
    }

    /**
     * @return the fn850
     */
    public int getFn850() {
        if (SessionProvider.getSessionMapValue("IDSENDSMSMAIL") != null) {
            fn850 = Util.parseInt(SessionProvider.getSessionMapValue("IDSENDSMSMAIL"));
        } else {
            fn850 = Util.parseInt(SessionProvider.getSessionMapValue("groupID"));
        }
        return fn850;
    }

    /**
     * @param fn850 the fn850 to set
     */
    public void setFn850(int fn850) {
        this.fn850 = fn850;
    }

    /**
     * @return the countRow
     */
    public int getCountRow() {
        return countRow;
    }

    /**
     * @param countRow the countRow to set
     */
    public void setCountRow(int countRow) {
        this.countRow = countRow;
    }

//     public static void main(String[] args) throws Exception {
//       
//        String message = "Nhan dip 15/06/2013 Sai Gon Travel kinh chuc Quy khach Mot chuyen di vui ve";
//        String receiver = "0935646648";
//       
//       String abc = SMSUTIL.sendSmSUtil(message, receiver);
//       if(abc.equals(PackageContants.smsSuccess)){
//           System.out.println("---------------"+abc);
//       }
//       System.out.println("---"+abc);
//       
//    }
//    public static void main(String[] args) throws Exception {
//       String contentmessage = "Nhan dip 15/06/2013 Sai Gon Travel kinh chuc Quy khach Mot chuyen di vui ve";
//       String numberPhone = "073564664852";
//       
//       String abc = SMSUTIL.sendSmSUtilFibo(numberPhone, contentmessage);
//      if(abc.equals(PackageContants.smsSuccess)){
//           System.out.println("---------------"+abc);
//       }
//       System.out.println("---"+abc);
//    }
    // Ham nhan sms cua akhch hang
//    public static void main(String[] args) throws Exception {
////       String contentmessage = "Nhan dip 15/06/2013 Sai Gon Travel kinh chuc Quy khach Mot chuyen di vui ve";
////       String numberPhone = "07356466489";
//       String formid = "568814";
//       Long longid = Long.valueOf(formid);
//       String abc = SMSUTIL.reviceSMSofCustomer(longid);
//      if(abc.equals(PackageContants.smsSuccess)){
//           System.out.println("---------------"+abc);
//       }
//       System.out.println("---"+abc);
//    }
    
    
//    private static String convertDocumentToString(Document doc) {
//        TransformerFactory tf = TransformerFactory.newInstance();
//        Transformer transformer;
//        try {
//            transformer = tf.newTransformer();
//            // below code to remove XML declaration
//            // transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//            StringWriter writer = new StringWriter();
//            transformer.transform(new DOMSource(doc), new StreamResult(writer));
//            String output = writer.getBuffer().toString();
//            return output;
//        } catch (TransformerException e) {
//            e.printStackTrace();
//        }
//         
//        return null;
//    }
// 
//    private static Document convertStringToDocument(String xmlStr) {
//        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
//        DocumentBuilder builder;  
//        try 
//        {  
//            builder = factory.newDocumentBuilder();  
//            Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) ); 
//            return doc;
//        } catch (Exception e) {  
//            e.printStackTrace();  
//        } 
//        return null;
//    }
    
//    public static void main(String[] args) {
//       String formid = "568814";
//       Long longid = Long.valueOf(formid);
//       String abc = SMSUTIL.reviceSMSofCustomer(longid);
//       System.out.println("---"+abc);
//        final String xmlStr = abc;
//        Document doc = convertStringToDocument(xmlStr);       
//        String str = convertDocumentToString(doc);
//        System.out.println("-------------------AAAAAAAAAAA-----------"+str);
//         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
//  
//        DocumentBuilder builder;  
//        try  
//        {  
//            builder = factory.newDocumentBuilder();  
//  
//            // Use String reader  
//            Document document = builder.parse( new InputSource(new StringReader(str)));  
//  
//            TransformerFactory tranFactory = TransformerFactory.newInstance();  
//            Transformer aTransformer = tranFactory.newTransformer();  
//            Source src = new DOMSource( document );  
//            Result dest = new StreamResult(new File("C:\\file.xml"));
//            aTransformer.transform( src, dest );  
//            abc("C:\\file.xml");
//        } catch (Exception e)  
//        {  
//            // TODO Auto-generated catch block  
//            e.printStackTrace();  
//        }  
//    }
    
//    public static  void abc(String file){
//         try {
//	File fXmlFile = new File(file);
//	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
//	DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//	Document doc = dBuilder.parse(fXmlFile);
//	//optional, but recommended
//	//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
//	doc.getDocumentElement().normalize();
//	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
//	NodeList nList = doc.getElementsByTagName("SMSLIST");
//	System.out.println("----------------------------");
//	for (int temp = 0; temp < nList.getLength(); temp++) {
//		Node nNode = nList.item(temp);
//		System.out.println("\nCurrent Element :" + nNode.getNodeName());
//		if (nNode.getNodeType() == Node.ELEMENT_NODE) {
//			Element eElement = (Element) nNode;	
//			System.out.println("ClientCommingSMSID :-------------- " + eElement.getElementsByTagName("ClientCommingSMSID").item(0).getTextContent());
//			System.out.println("PhoneNumber :---------------- " + eElement.getElementsByTagName("PhoneNumber").item(0).getTextContent());
//			System.out.println("message -----------------: " + eElement.getElementsByTagName("message").item(0).getTextContent());
//			System.out.println("SentDate :--------------------- " + eElement.getElementsByTagName("SentDate").item(0).getTextContent());
//                        System.out.println("ClientCommingSMSHostingStatus :--------------------- " + eElement.getElementsByTagName("ClientCommingSMSHostingStatus").item(0).getTextContent());
//                        System.out.println("createddate :--------------------- " + eElement.getElementsByTagName("createddate").item(0).getTextContent());
//                        System.out.println("ServiceTypeID :--------------------- " + eElement.getElementsByTagName("ServiceTypeID").item(0).getTextContent());
//                        System.out.println("serviceTypename :--------------------- " + eElement.getElementsByTagName("serviceTypename").item(0).getTextContent());
// 
//		}
//	}
//    } catch (Exception e) {
//	e.printStackTrace();
//    }
//    }
    
 

}
